package com.example.demo.exception;


import com.example.demo.bean.AjaxRes;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 所有异常
     * @param e 异常类
     * @return 返回值
     */
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<AjaxRes> handleAllException(Exception e){
        return ResponseEntity.status(HttpStatus.OK).body(AjaxRes.error(e.getMessage()));
    }

    /**
     * 自定义基础异常
     * @param e 异常类
     * @return
     */
    @ExceptionHandler(value = OtherException.class)
    public ResponseEntity<AjaxRes> handleBaseException(Exception e){
        return ResponseEntity.status(HttpStatus.OK).body(AjaxRes.error(e.getMessage()));
    }

    /**
     * token异常
     * @param e 异常类
     * @return
     */
    @ExceptionHandler(value = TokenException.class)
    public ResponseEntity<AjaxRes> handleTokenException(TokenException e){
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(AjaxRes.error(e.getMessage()));
    }
}
